/*
 * Copyright 2013 Stanley Shyiko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.shyiko.mysql.binlog.event.deserialization;

import com.github.shyiko.mysql.binlog.event.TableMapEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
import com.rong360.binlogutil.GlobalConfig;

import java.io.IOException;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
 */
public class WriteRowsEventDataDeserializer extends AbstractRowsEventDataDeserializer<WriteRowsEventData> {

	private boolean mayContainExtraInformation;

	public WriteRowsEventDataDeserializer(Map<Long, TableMapEventData> tableMapEventByTableId) {
		super(tableMapEventByTableId);
	}

	public WriteRowsEventDataDeserializer setMayContainExtraInformation(boolean mayContainExtraInformation) {
		this.mayContainExtraInformation = mayContainExtraInformation;
		return this;
	}

	@Override
	public WriteRowsEventData deserialize(ByteArrayInputStream inputStream) throws IOException {
		WriteRowsEventData eventData = new WriteRowsEventData();
		eventData.setTableId(inputStream.readLong(6));
		if(!GlobalConfig.filterByTableId(eventData.getTableId())){
			return null;
		}
		inputStream.skip(2); // reserved
		if (mayContainExtraInformation) {
			int extraInfoLength = inputStream.readInteger(2);
			inputStream.skip(extraInfoLength - 2);
		}
		int numberOfColumns = inputStream.readPackedInteger();
		eventData.setIncludedColumns(inputStream.readBitSet(numberOfColumns, true));
		eventData.setRows(deserializeRows(eventData, inputStream));
		return eventData;
	}

	private List<Serializable[]> deserializeRows(WriteRowsEventData eventData, ByteArrayInputStream inputStream)
			throws IOException {
		TableMapEventData tableMapEvent = getTableMapEventData(eventData.getTableId());
		ColumnSet includedColumns = new ColumnSet(eventData.getIncludedColumns());
		List<Serializable[]> result = new LinkedList<Serializable[]>();
		while (inputStream.available() > 0) {
			result.add(deserializeRow(tableMapEvent, includedColumns, inputStream));
		}
		return result;
	}

}
